﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 公云动态更新
{
    public partial class FormGongYun : Form
    {
        public FormGongYun()
        {
            InitializeComponent();
        }

        private string strUrl_web = @"http://members.3322.net";
        private string strUrl_web_getip = @"http://ip.3322.net";
        private string hostname = "gfun.f3322.org";
        private string ip = "110.90.246.179";


        private void Form1_Load(object sender, EventArgs e)
        {
            System.Timers.Timer timer_SendRequest = new System.Timers.Timer();//定时周期1秒
            timer_SendRequest.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed);//到2秒了做的事件
            timer_SendRequest.Interval = 1000;
            timer_SendRequest.Enabled = true;
            timer_SendRequest.AutoReset = true; //是否不断重复定时器操作
        }

        private void CallRequest()
        {
            RequestGetIP();
            RequestUpdateDomainIP();
        }

        private void RequestGetIP()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl_web_getip);
            request.Method = "get";
            HttpWebResponse loginresponse = (HttpWebResponse)request.GetResponse();
            StreamReader responsestream = new StreamReader(loginresponse.GetResponseStream(), Encoding.UTF8);
            string responsetext = responsestream.ReadToEnd();
            ip = responsetext;

            if (this.label_resttime.InvokeRequired)
            {
                iplabel_text = "IP:" + ip;
                this.Invoke(new SetRestTimeLableTextCallBack(SetIpLabelText));
            }
            else
            {
                label_ip.Text = "IP:" + ip;
            }
            
        }
        private void RequestUpdateDomainIP()
        {
            string uri = strUrl_web + string.Format("/dyndns/update?hostname={0}&myip={1}&wildcard=OFF", hostname, ip);

            NetworkCredential logincredentials = new NetworkCredential("super999", "chenxiawen");
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "get";
            request.KeepAlive = true;
            request.Credentials = logincredentials;

            HttpWebResponse loginresponse = (HttpWebResponse)request.GetResponse();
            label_status.Text = "状态：" + loginresponse.StatusCode.ToString();
            StreamReader responsestream = new StreamReader(loginresponse.GetResponseStream(), Encoding.UTF8);
            string responsetext = responsestream.ReadToEnd();
            label_response.Text = "返回结果:" + responsetext;

            updateTime = DateTime.Now.ToString();
        }
        private int resttime = 60;
        private string updateTime;
        private string resttime_text;
        private string iplabel_text;
        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (this.label_resttime.InvokeRequired)
            {
                resttime_text = "剩余时间:" + resttime + "秒 " + updateTime;

                this.Invoke(new SetRestTimeLableTextCallBack(SetRestTimeLableText));
            }
            else
            {
                label_resttime.Text = "剩余时间:" + resttime + "秒 " + updateTime;
            }

            resttime--;
            if (resttime < 0)
            {
                resttime = 60;
                this.Invoke(new SetRestTimeLableTextCallBack(CallRequest));
            }
        }

        private delegate void SetRestTimeLableTextCallBack();

        private void SetRestTimeLableText()
        {
            label_resttime.Text = resttime_text;
        }
        private void SetIpLabelText()
        {
            label_ip.Text = iplabel_text;
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                MiniTask();
            }
        }

        private void MiniTask()
        {
            this.notifyIcon1.Visible = true;//在通知区显示Form的Icon
            this.WindowState = FormWindowState.Minimized;
            this.Visible = false;
            this.ShowInTaskbar = false;//使Form不在任务栏上显示
        }
        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
        }

        private void button_mini_Click(object sender, EventArgs e)
        {
            MiniTask();
        }

    }
}
